package com.study.meng.bigevent.service.impl;

import com.study.meng.bigevent.common.utils.ThreadLocalUtil;
import com.study.meng.bigevent.entity.Category;
import com.study.meng.bigevent.entity.User;
import com.study.meng.bigevent.mapper.CategoryMapper;
import com.study.meng.bigevent.mapper.UserMapper;
import com.study.meng.bigevent.service.ArticleService;
import com.study.meng.bigevent.service.CateGoryService;
import com.study.meng.bigevent.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CateGoryServiceImpl implements CateGoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ArticleService articleService;
    @Override
    public boolean add(Category category) {
        return categoryMapper.insert(category) > 0; // Assuming insert method exists in CategoryMapper
    }

    @Override
    public List<Category> list() {
        Integer userId = ThreadLocalUtil.getCurrentUser();
        return categoryMapper.selectAll(userId); // Assuming selectAll method exists in CategoryMapper
    }

    @Override
    public Category detail(Integer id) {
        return categoryMapper.selectById(id); // Assuming selectById method exists in CategoryMapper
    }

    @Override
    public boolean update(Category category) {
        return categoryMapper.update(category) > 0; // Assuming update method exists in CategoryMapper
    }

    @Override
    public boolean delete(Integer id) {
        //查询分类下是否有文章
        Integer count = articleService.countByCategoryId(id);
        if (count > 0) {
            return false; // Cannot delete category with existing articles
        }
        return categoryMapper.delete(id) > 0; // Assuming delete method exists in CategoryMapper
    }
}
